import 'package:dio/dio.dart';

class DioInstance {
// static _instance，_instance会在编译期被初始化，保证了只被创建一次
  static final DioInstance _instance = DioInstance._internal();

  //提供了一个工厂方法来获取该类的实例
  factory DioInstance() {
    return _instance;
  }

  // 通过私有方法_internal()隐藏了构造方法，防止被误创建
  DioInstance._internal() {
    // 初始化
    init();
  }

  late Dio dio;

  // Singleton._internal(); // 不需要初始化

  void init() {
    print("DioInstance这里初始化");
    dio = Dio();
    dio.options.headers.addAll({
      "Access-Control-Allow-Origin": "*",
      // Required for CORS support to work
      "Access-Control-Allow-Credentials": true,
      // Required for cookies, authorization headers with HTTPS
      "Access-Control-Allow-Headers":
          "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale",
      "Access-Control-Allow-Methods": "POST,GET, OPTIONS"
    });
  }
}
